#include <cctype>
#include <cassert>
#include <cstring>
#include <strings.h>

/* Copy a string from src to dest, returning a pointer to
   the end of the resulting string at dest. */
char *mystpcpy(char *dest, const char *src)
{
    int d=0;
    while(src[d]!=0){
    dest[d]=src[d];
    d++;
}
dest[d]=0;
return dest;
}

/* Compare the strings s1 and s2 ignoring case. */
int mystrcasecmp(const char *s1, const char *s2)
{
    int a=0;
    char b;
    char c;
    while(s1[a]!=0){
    b=s1[a];
    c=s2[a];
    if(b>='A'&& b<= 'Z')
    b+='a'-'A';
    if(c>='A'&& b<='Z')
    c+='a'-'A';
    if (b!=c)
    return 32;
    a++;
}
return 0;
}

/* Compare the first n bytes of the strings s1 and s2 ignoring case. */
int mystrncasecmp(const char *s1, const char *s2, size_t n)
{
 int a=0;
    char b;
    char c;
    while(s1[a]!=0&&a<n){
    b=s1[a];
    c=s2[a];
    if(b>='A'&& b<= 'Z')
    b+='a'-'A';
    if(c>='A'&& b<='Z')
    c+='a'-'A';
    if (b!=c)
    return 3;
    a++;
}
return 0;
}

int main()
{
    char buf1[100], buf2[100];
    const char *hello1 = "Hello, world!";

    char *result = mystpcpy(buf1, hello1);
    char *expected = ::stpcpy(buf2, hello1);

    assert(result == (buf1 + strlen(hello1)));
    assert(expected == (buf2 + strlen(hello1)));
    assert(::strcmp(buf1, buf2) == 0);

    const char *hello2 = "hello, world.";
    assert(mystrcasecmp(hello1, hello2) == ::strcasecmp(hello1, hello2));
    assert(mystrncasecmp(hello1, hello2, 5) == ::strncasecmp(hello1, hello2, 5));
}
